package service;

import dao.db;
import dao.redis;
import pojo.Guestroominfo;
import pojo.User;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserService {
    public static String login(User u) {
        //先去redis查询
        User userRe = (User) redis.get("user" + u.getId());
        userRe = null;
        //redia查到了，就直接通过
        if (userRe != null && userRe.getMpassword().equals(u.getMpassword())) {
            System.out.println("redis查询");
            return userRe.getMlimits();
        } else {
            //没有再去数据库查
            db d = new db();
            String sql = "select * from manager where id=" + u.getId() +
                    " and mpassword=" + u.getMpassword();
            ResultSet resultSet = d.executeQuery(sql);
            User dbu = (User) db.selectOneObject(new User(), resultSet);
            if (dbu != null) {
                assert dbu != null;
                redis.set("user" + dbu.getId(), dbu);
                return dbu.getMlimits();
            } else {
                return "0";
            }
        }


    }

    public static List<User> all_user() {
        db d = new db();
        String sql = "select * from manager";
        ResultSet resultSet = d.executeQuery(sql);
        List<Object> list = db.selectList(new User(), resultSet);
        List<User> list1 = new ArrayList<>();
        list.forEach(i -> {
            list1.add((User) i);
        });
        return list1;
    }

    public static int deleteByid(int id) {
        db d = new db();
        String sql = "delete from manager where id=" + id;
        return d.update_execute(sql);
    }

    public static int updateByid(Guestroominfo Guestroominfo) {
        db d = new db();
        String sql = "";
        return d.update_execute(sql);
    }

    public static int insert(User user) {
        db d = new db();
        String sql = "INSERT INTO hotel.manager (mpassword, mlimits)\n" +
                "VALUES ('" + user.getMpassword() +
                "', '" + user.getMlimits() +
                "');";
        System.out.println(sql);
        return d.update_execute(sql);
    }
}
